var app = new Vue({
	el:"#app",
	data:function() {
		return {
			title:"Hello Vue",
			tableData:null,
			currentPage:1,
			pageSize:20,
			total:0,
			tableHeight:(document.documentElement.clientHeight-110),
			contextPath:"",
			showList:true,
			dialogTableVisible:false,
			dialogTitle:'新增',
			fields:[
								{
					prop:"id",
					label:"主键"
				},
				{
					prop:"supplierType",
					label:"供应商类型：1表示个人,2表示公司"
				},
				{
					prop:"supplierName",
					label:"供应商名称"
				},
				{
					prop:"supplierNumber",
					label:"供应商编号"
				},
				{
					prop:"contactsName",
					label:"联系人名称"
				},
				{
					prop:"contactsPhone",
					label:"联系人电话"
				},
				{
					prop:"contactsQq",
					label:"联系人QQ"
				},
				{
					prop:"createUserId",
					label:"创建人"
				},
				{
					prop:"createDate",
					label:"创建时间"
				},
				{
					prop:"updateUserId",
					label:"更新人"
				},
				{
					prop:"updateDate",
					label:"更新时间"
				},
				{
					prop:"status",
					label:"状态"
				},
				{
					prop:"remark",
					label:"备注"
				},
				{
					prop:"delFlag",
					label:"删除标志：1正常，0删除"
				},
			],
			form: {
				id:'',
				supplierType:"1",
				supplierName:'',
				supplierNumber:'',
				contactsName:'',
				contactsPhone:'',
				contactsQq:'',
				createUserId:'',
				createDate:'',
				updateUserId:'',
				updateDate:'',
				status:'',
				remark:'',
				delFlag:'',
	        },
	        multipleSelection:[],
			searchForm:{
				name:''
			},
			loading:true
		}
	},
	methods:{
		handleCurrentChange:function(val) {
			this.currentPage = val;
			this.getList();
		},
		handleSizeChange:function(val) {
			this.pageSize = val;
			this.getList();
		},
		//新增数据
		addForm:function() {
			//显示弹出框
			this.dialogTableVisible = true;
			this.dialogTitle = "新增";
			this.form = {
				id:'',
				supplierType:"1",
				supplierName:'',
				supplierNumber:'',
				contactsName:'',
				contactsPhone:'',
				contactsQq:'',
				createUserId:'',
				createDate:'',
				updateUserId:'',
				updateDate:'',
				status:'',
				remark:'',
				delFlag:'',
			};
			this.value2 = [];
		},
		//删除多条列表项
		deleteItems:function() {
			if (this.multipleSelection.length == 0) {
				this.$alert("至少选择一条记录", "提示",{
					type:"warning"
				});
			} else {
				let that = this;
				this.$confirm('此操作将永久删除数据, 是否继续?', '提示', {
		          confirmButtonText: '确定',
		          cancelButtonText: '取消',
		          type: 'warning'
		        }).then(() => {
					let ids = [];
					that.multipleSelection.forEach(function(item,index){
						ids.push(item.id);
					});
					axios.put(that.contextPath + "/inventory/supplier/batchDelete",{
						ids:JSON.stringify(ids)
					}).then(function(){
						that.$message({
							type: 'success',
							message: '删除成功',
							duration:'1000'
						});
					that.getList();
					}).catch(function(){
						that.$message({
							type: 'waring',
							message: '删除失败',
							duration:'1000'
						});
					});
		        }).catch(() => {
		          this.$message({
		            type: 'info',
		            message: '已取消删除',
		            duration:'1000'
		          });          
		        });
			}
		},
		//编辑数据
		editItem:function(row) {
			//显示弹出框
			this.dialogTableVisible = true;
			this.dialogTitle = "编辑";
			this.form = {
				id:row.id,
				supplierType:row.supplierType,
				supplierName:row.supplierName,
				supplierNumber:row.supplierNumber,
				contactsName:row.contactsName,
				contactsPhone:row.contactsPhone,
				contactsQq:row.contactsQq,
				createUserId:row.createUserId,
				createDate:row.createDate,
				updateUserId:row.updateUserId,
				updateDate:row.updateDate,
				status:row.status,
				remark:row.remark,
				delFlag:row.delFlag,
			};
		},
		//删除单条列表项
		deleteItem:function(id) {
			let that = this;
			this.$confirm('此操作将永久删除数据, 是否继续?', '提示', {
		          confirmButtonText: '确定',
		          cancelButtonText: '取消',
		          type: 'warning'
		        }).then(() => {
					axios.post(this.contextPath + "/inventory/supplier/delete",{
						"id":id
					}).then(function(response){
						that.$message({
							type: 'success',
							message: '删除成功!',
							duration:'1000'
						});
						that.getList();
					}).catch(function(){
						that.$message({
							type: 'warning',
							message: '删除失败，请重试',
							duration:'1000'
						});
					});
		        }).catch(() => {
		          this.$message({
		            type: 'info',
		            message: '已取消删除',
		            duration:'1000'
		          });          
		        });
		},
		//保存表单数据
		onSubmit:function() {
			let that = this;
			axios.post(this.contextPath + "/inventory/supplier/save",this.form)
				.then(function(){
					that.$message({
					type: 'success',
					message: '保存成功!',
					duration:'1000'
				});
				that.dialogTableVisible = false;
				that.getList();
				})
				.catch(function(){
					that.$message({
						type: 'warning',
						message: '保存失败!',
						duration:'1000'
					});
				});
		},
		//表单取消提交操作
		cancelForm:function() {
			this.dialogTableVisible = false;
		},
		//列表选中行发生改变时
		handleSelectionChange:function(val) {
			this.multipleSelection = val;
		},
		//根据当前页和大小获得分页列表数据
		getList:function() {
			let that = this;
			that.loading = true;
			axios.post(this.contextPath + "/inventory/supplier/list/" + this.currentPage + "/" + this.pageSize,{
					"supplierName":that.searchForm.name,
					"supplierNumber":that.searchForm.name
				})
				.then(function(response){
					that.total = response.data.total;
					that.tableData = response.data.records;
					that.loading = false;
				})
				.catch(function(){
					that.loading = false;
				});
		},
		searchClick:function() {
			this.currentPage = 1;
			this.getList();
		},
		//日期格式化
        dataFormat:function (row, column, cellValue, index) {
			return moment(cellValue,"YYYY-MM-DD").format("YYYY-MM-DD");
        },
		//供应商类型格式化
        supplierTypeFormat:function (row, column, cellValue, index) {
			if (cellValue == "1") {
				return "个人";
			} else {
				return "公司";
			}
        }
	},
	mounted:function() {
		this.contextPath = this.$refs.contextPath.value;
		this.getList();
	}
});
